package model

import (
	"gorm.io/gorm"
)

// Medicine 药品模型
type Medicine struct {
	gorm.Model
	Name             string  `gorm:"type:varchar(100);not null;comment:药品名称"`
	Category         string  `gorm:"type:varchar(50);comment:分类"`
	Efficacy         string  `gorm:"type:text;comment:功效"`
	Contraindication string  `gorm:"type:text;comment:禁忌"`
	SideEffect       string  `gorm:"type:text;comment:副作用"`
	Price            float64 `gorm:"type:decimal(10,2);not null;comment:价格"`
	ImageUrl         string  `gorm:"type:varchar(255);comment:图片URL"`
	Stock            int     `gorm:"type:int;not null;default:0;comment:库存"`
	Status           int     `gorm:"type:tinyint(1);not null;default:1;comment:状态:1-上架,2-下架"`
}

// Prescription 处方模型
type Prescription struct {
	gorm.Model
	PrescriptionNo string  `gorm:"type:varchar(32);not null;unique;comment:处方号"`
	ConsultationID int64   `gorm:"type:bigint;not null;comment:问诊ID"`
	DoctorID       int64   `gorm:"type:bigint;not null;comment:医生ID"`
	PatientID      int64   `gorm:"type:bigint;not null;comment:患者ID"`
	TotalAmount    float64 `gorm:"type:decimal(10,2);comment:总金额"`
	AuditStatus    int     `gorm:"type:tinyint(1);not null;default:0;comment:审核状态:0-待审核,1-通过,2-驳回"`
	AuditReason    string  `gorm:"type:varchar(255);comment:驳回原因"`
}

// PrescriptionItem 处方明细
type PrescriptionItem struct {
	gorm.Model
	PrescriptionID int64   `gorm:"type:bigint;not null;index;comment:处方ID"`
	MedicineID     int64   `gorm:"type:bigint;not null;comment:药品ID"`
	MedicineName   string  `gorm:"type:varchar(100);not null;comment:药品名称"`
	Quantity       int     `gorm:"type:int;not null;comment:数量"`
	Dosage         string  `gorm:"type:varchar(200);comment:用法用量"`
	Price          float64 `gorm:"type:decimal(10,2);comment:单价"`
}

// MedicineOrder 药品订单
type MedicineOrder struct {
	gorm.Model
	OrderNo         string  `gorm:"type:varchar(32);not null;unique;comment:订单号"`
	PrescriptionID  int64   `gorm:"type:bigint;not null;comment:处方ID"`
	PatientID       int64   `gorm:"type:bigint;not null;comment:患者ID"`
	TotalPrice      float64 `gorm:"type:decimal(10,2);comment:总价"`
	DeliveryAddress string  `gorm:"type:varchar(255);comment:配送地址"`
	Status          int     `gorm:"type:tinyint(1);not null;default:1;comment:状态:1-已下单,2-配送中,3-已完成,4-已取消"`
}

// GetMedicineList 获取药品列表
func GetMedicineList(db *gorm.DB, page, pageSize int, keyword, category string) ([]Medicine, int64, error) {
	var medicines []Medicine
	var total int64

	query := db.Model(&Medicine{}).Where("status = ?", 1)

	if keyword != "" {
		query = query.Where("name LIKE ?", "%"+keyword+"%")
	}
	if category != "" {
		query = query.Where("category = ?", category)
	}

	// 统计总数
	if err := query.Count(&total).Error; err != nil {
		return nil, 0, err
	}

	// 使用分页作用域
	if err := query.Scopes(Paginate(page, pageSize)).Find(&medicines).Error; err != nil {
		return nil, 0, err
	}

	return medicines, total, nil
}

// GetMedicineByID 根据ID查询药品
func (m *Medicine) GetMedicineByID(db *gorm.DB, id int64) error {
	return db.Where("id = ?", id).First(&m).Error
}

// CreatePrescription 创建处方
func (p *Prescription) CreatePrescription(db *gorm.DB) error {
	return db.Create(&p).Error
}

// GetPrescriptionByID 根据ID查询处方
func (p *Prescription) GetPrescriptionByID(db *gorm.DB, id int64) error {
	return db.Where("id = ?", id).First(&p).Error
}

// CreateMedicineOrder 创建药品订单
func (o *MedicineOrder) CreateMedicineOrder(db *gorm.DB) error {
	return db.Create(&o).Error
}

// GetMedicineOrderList 获取药品订单列表
func GetMedicineOrderList(db *gorm.DB, patientId int64, page, pageSize int) ([]MedicineOrder, int64, error) {
	var orders []MedicineOrder
	var total int64

	query := db.Model(&MedicineOrder{}).Where("patient_id = ?", patientId)

	// 统计总数
	if err := query.Count(&total).Error; err != nil {
		return nil, 0, err
	}

	// 使用分页作用域
	if err := query.Scopes(Paginate(page, pageSize)).Order("created_at DESC").Find(&orders).Error; err != nil {
		return nil, 0, err
	}

	return orders, total, nil
}

